package com.items;


import com.items.TreeUtil.ITreeUtilImpl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


public class TreeDemo {

    static List<Test> testList = new ArrayList<Test>(){{
        add(new Test("1","一",null));
        add(new Test("2","一",null));
        add(new Test("3","一",null));
        add(new Test("1.1","一","1"));
        add(new Test("1.2","一","1"));
        add(new Test("1.1.1","一","1.1"));
        add(new Test("2.1","一","2"));
    }};

    static class Test{
        String id;
        String name;
        String Pid;
        List<Test> children;

        public Test(String id, String name, String pid) {
            this.id = id;
            this.name = name;
            Pid = pid;
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getPid() {
            return Pid;
        }

        public void setPid(String pid) {
            Pid = pid;
        }

        public List<Test> getChildren() {
            return children;
        }

        public void setChildren(List<Test> children) {
            this.children = children;
        }

        @Override
        public String toString() {
            return "Test{" +
                    "id='" + id + '\'' +
                    ", name='" + name + '\'' +
                    ", Pid='" + Pid + '\'' +
                    ", children=" + children +
                    '}';
        }
    }


    public static void main(String[] args) {

        ITreeUtilImpl<Test, String> treeUtil = new ITreeUtilImpl<>();
        treeUtil.setBoList(testList);
        treeUtil.setCodeFunc(Test::getId);
        treeUtil.setParentFunc(Test::getPid);
        treeUtil.setListFunc(Test::getChildren);
        treeUtil.setConsumer(Test::setChildren);
        List<Test> trees = treeUtil.getTreeList(testList.stream().filter(item-> item.getPid() == null).collect(Collectors.toList())
                );
        System.out.println(trees);
    }

    
}
